//
//  DynamicColumnFieldCore+OutputData.m
//  
//
//  Created by wjxfhxy on 15/12/30.
//
//

#import "DynamicColumnFieldCore+OutputData.h"

#import "DynamicColumnFieldCore+Internal.h"

@implementation DynamicColumnFieldCore (OutputData)

- (XLGResult*)verifyDynamicField:(XMSQueueDictionary*)constraint fromData:(id(^)(NSString *fieldName))fromData
{
    NSString *errorStr = @"";
    
    for(XMSKvPair *pair in constraint)
    {
        for(FieldDescript *fixField in pair.idValue)
        {
            id checkData = fromData(fixField.fieldName);
            
            switch(fixField.controlType.intValue)
            {
                case DynamicFieldControlTypeText:
                case DynamicFieldControlTypeBigText:
                case DynamicFieldControlTypeInt:
                case DynamicFieldControlTypeDecimal:
                case DynamicFieldControlTypeTextSingleSelect:
                {
                    NSString *text = checkData;
                    if(checkData == nil || [checkData isKindOfClass:[NSNull class]])
                        text = @"";
                    else if([checkData isKindOfClass:[NSString class]])
                        text = checkData;
                    else if([checkData isKindOfClass:[XMSNumber class]])
                        text = [(XMSNumber*)checkData toNSString];
                    else if([checkData isKindOfClass:[NSNumber class]])
                        text = [(NSNumber*)checkData stringValue];
                    
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        if(text.length <= 0 || [text isBlankString])
                        {
                            errorStr = [NSString stringWithFormat:@"请输入%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    
                    if(text.length > 0 )
                    {
                        BOOL b = YES;
                        switch(fixField.controlType.intValue)
                        {
                            case DynamicFieldControlTypeInt:
                            {
                                b = [text isNumberString];
                                break;
                            }
                            case DynamicFieldControlTypeDecimal:
                            {
                                b = [text isFloatNumberString];
                                break;
                            }
                        }
                        if(b == NO)
                        {
                            errorStr = [NSString stringWithFormat:@"请输入正确的%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                        if(fixField.fieldLength != nil && text.length > fixField.fieldLength.intValue)
                        {
                            errorStr = [NSString stringWithFormat:@"%@最长%@个字符",fixField.fieldLabel,[fixField.fieldLength toNSString]];
                            goto FunExit;
                        }
                        if(fixField.regx.length >0)
                        {
                            NSRegularExpression *regxExp = [NSRegularExpression regularExpressionWithPattern:fixField.regx options:NSRegularExpressionCaseInsensitive error:nil];
                            NSArray* match = [regxExp matchesInString:text options:NSMatchingReportCompletion range:NSMakeRange(0, text.length)];
                            if(match.count <= 0)
                            {
                                errorStr = [NSString stringWithFormat:@"请输入正确的%@",fixField.fieldLabel];
                                goto FunExit;
                            }
                            NSTextCheckingResult *selectResult = [match objectAtIndex:0];
                            if(selectResult.range.location != 0)
                            {
                                errorStr = [NSString stringWithFormat:@"请输入正确的%@",fixField.fieldLabel];
                                goto FunExit;
                            }
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeSingleSelect:
                case DynamicFieldControlTypeSingleSelectDic:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        NSString *text = checkData;
                        if(([text isKindOfClass:[NSString class]] && text.length <= 0) || text == nil)
                        {
                            errorStr = [NSString stringWithFormat:@"请选择%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeMultiSelect:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        NSArray * ar = checkData;
                        if(ar.count <= 0)
                        {
                            errorStr = [NSString stringWithFormat:@"请选择%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypePosition:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        NSString *text = checkData;
                        if(text.length <= 0)
                        {
                            errorStr = [NSString stringWithFormat:@"请输入%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeDate:
                case DynamicFieldControlTypeDateTime:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        NSString *text = checkData;
                        if(([text isKindOfClass:[NSString class]] && text.length <= 0) || text == nil)
                        {
                            errorStr = [NSString stringWithFormat:@"请选择%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeImagePanel:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        NSArray *data = checkData;
                        if([data isKindOfClass:[NSArray class]] && data.count <= 0)
                        {
                            errorStr = [NSString stringWithFormat:@"请选择%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeSwitchSelect:
                {
                    if(fixField.isAllowEmpty.intValue == 0)
                    {
                        if(checkData == nil)
                        {
                            errorStr = [NSString stringWithFormat:@"请选择%@",fixField.fieldLabel];
                            goto FunExit;
                        }
                    }
                    break;
                }
            }
        }
    }
    
FunExit:
    
    if(errorStr.length > 0)
        return [XLGResult resultWithOutResult:NO outObj:nil outError:[NSError errorWithDomain:errorStr code:0 userInfo:nil]];
    
    return [XLGResult resultWithOutResult:YES];
}

- (BOOL)verifyDynamicField:(XMSQueueDictionary*)constraint
{
    XLGResult *r = [self verifyDynamicField:constraint fromData:^id(NSString *fieldName)
    {
        id data = nil;
        XUIGroupTableViewCell *cell = [_gtable cellWithTag:fieldName];
        if([cell isMemberOfClass:[DateGroupTableViewCell class]])
        {
            data = ((DateGroupTableViewCell*)cell).text;
        }
        else if([cell isMemberOfClass:[PositionGroupTableViewCell class]])
        {
            data = ((PositionGroupTableViewCell*)cell).text;
        }
        else if([cell isMemberOfClass:[TextAreaGroupTableViewCell class]])
        {
            data = ((TextAreaGroupTableViewCell*)cell).text;
        }
        else if([cell isMemberOfClass:[MultiSelectGroupTableViewCell class]])
        {
            data = ((MultiSelectGroupTableViewCell*)cell).selectitems;
        }
        else if([cell isKindOfClass:[DropDownGroupTableViewCell class]])
        {
            data = ((DropDownGroupTableViewCell*)cell).text;
        }
        else if([cell isKindOfClass:[TextInputGroupTableViewCell class]])
        {
            data = ((TextInputGroupTableViewCell*)cell).text;
        }
        
        return data;
    }];
    
    if(r.outResult != YES && r.outError.domain.length > 0)
    {
        [UIAlertView showInfoMsg:r.outError.domain];
    }
    
    return r.outResult;
}

- (void)packObj:(id)packObj extractTextValue:(NSString*)text fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom )
        [packObj setValue:text forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
    else
        [packObj expandfields][fieldDescript.fieldName] = text;
}

- (void)packObj:(id)packObj extractIntValue:(NSString*)text fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom )
        [packObj setValue:text.length > 0 ? [XMSNumberLongLong numberLongLongWithLongLongValue:[text longLongValue]] : [NSNull null] forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
    else
        [packObj expandfields][fieldDescript.fieldName] = text.length > 0 ? @([text longLongValue]) : [NSNull null];
}

- (void)packObj:(id)packObj extractDecimalValue:(NSString*)text fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom )
    {
        NSString *type = [packObj getPropertyType:[transformMap getValueForKey:fieldDescript.fieldName]];
        if([type isEqualToString:@"NSString"])
        {
            [packObj setValue:[NSString stringValue:text] forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
        }
        else
        {
            [packObj setValue:text.length > 0 ?[XMSNumberDouble numberDoubleWithDoubleValue:[text doubleValue]] : [NSNull null] forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
        }
    }
    else
    {
        [packObj expandfields][fieldDescript.fieldName] = text.length > 0 ? @([text doubleValue]) : [NSNull null];
    }
}

- (void)packObj:(id)packObj extractDateValue:(NSString*)text fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom )
        [packObj setValue:text.length > 0 ? [text toNSDate:NSDateFormatByyMd] : [NSNull null] forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
    else
        [packObj expandfields][fieldDescript.fieldName] = text;
}

- (void)packObj:(id)packObj extractDateTimeValue:(NSString*)text fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom )
        [packObj setValue:text.length > 0 ? [text toNSDate:NSDateFormatByyMdHm] : [NSNull null] forKey:[transformMap getValueForKey:fieldDescript.fieldName]];
    else
        [packObj expandfields][fieldDescript.fieldName] = text;
}

- (void)packObj:(id)packObj extractPostionValue:(NSString*)postionValue selectValue:(NSString*)selectValue fieldDescript:(FieldDescript*)fieldDescript transformMap:(XMSQueueDictionary*)transformMap
{
    NSString *selectAddress = selectValue;
    if(postionValue.length <= 0)
    {
        selectAddress = @"";
    }
    else
    {
        NSArray *ar = [selectValue componentsSeparatedByString:@";"];
        if(ar.count == 3)
            selectAddress = [NSString stringWithFormat:@"%@;%@;%@",ar[0],ar[1],postionValue];
        else
            selectAddress = [NSString stringWithFormat:@";;%@",postionValue];
    }
    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
        [packObj setValue:selectAddress forKeyPath:[transformMap getValueForKey:fieldDescript.fieldName]];
    else
        [packObj expandfields][fieldDescript.fieldName] = selectAddress;
}

- (void)extractDynamicFieldValue:(XMSQueueDictionary*)constraint transformMap:(XMSQueueDictionary*)transformMap packObj:(id)packObj specialExtract:(BOOL(^)(FieldDescript *fieldDescript))specialExtract
{
    XMSQueueDictionary *qdSet = transformMap;
    
    //清除原来扩展属性中所有值，防止不可见属性造成的错误
    //if([packObj respondsToSelector:@selector(setExpandfields:)])
    //    [packObj setExpandfields:[NSMutableDictionary new]];
    
    for(XMSKvPair *pair in constraint)
    {
        for(FieldDescript *fieldDescript in pair.idValue)
        {
            XUIGroupTableViewCell *cell = [_gtable cellWithTag:fieldDescript.fieldName];
            switch(fieldDescript.controlType.intValue)
            {
                case DynamicFieldControlTypeHidden:
                case DynamicFieldControlTypeText:
                case DynamicFieldControlTypeBigText:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];
                        [self packObj:packObj extractTextValue:text fieldDescript:fieldDescript transformMap:transformMap];
                    }
                    break;
                }
                case DynamicFieldControlTypeTextSingleSelect:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];
                        
                        switch(fieldDescript.capacity)
                        {
                            case DynamicFieldControlTypePosition:
                            {
                                NSString *selectAddress = [[NSString stringValue:((DropDownTextGroupTableViewCell*)cell).selectText] copy];
                                NSString *text = [[NSString stringValue:((DropDownTextGroupTableViewCell*)cell).text] copy];
                                
                                [self packObj:packObj extractPostionValue:text selectValue:selectAddress fieldDescript:fieldDescript transformMap:transformMap];
                                
                                break;
                            }
                            case DynamicFieldControlTypeInt:
                            {
                                [self packObj:packObj extractIntValue:text fieldDescript:fieldDescript transformMap:transformMap];
                                break;
                            }
                            case DynamicFieldControlTypeDecimal:
                            {
                                [self packObj:packObj extractDecimalValue:text fieldDescript:fieldDescript transformMap:transformMap];
                                break;
                            }
                            case DynamicFieldControlTypeDate:
                            {
                                [self packObj:packObj extractDateValue:text fieldDescript:fieldDescript transformMap:transformMap];
                                break;
                            }
                            case DynamicFieldControlTypeDateTime:
                            {
                                [self packObj:packObj extractDateTimeValue:text fieldDescript:fieldDescript transformMap:transformMap];
                                break;
                            }
                            default:
                            {
                                [self packObj:packObj extractTextValue:text fieldDescript:fieldDescript transformMap:transformMap];
                                break;
                            }
                        }
                    }
                    break;
                }
                case DynamicFieldControlTypeInt:
                {
                    NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];

                    [self packObj:packObj extractIntValue:text fieldDescript:fieldDescript transformMap:transformMap];

                    break;
                }
                case DynamicFieldControlTypeDecimal:
                {
                    NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];
                    
                    [self packObj:packObj extractDecimalValue:text fieldDescript:fieldDescript transformMap:transformMap];
                    
                    break;
                }
                case DynamicFieldControlTypeDate:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];
                        
                        [self packObj:packObj extractDateValue:text fieldDescript:fieldDescript transformMap:transformMap];
                    }
                    break;
                }
                case DynamicFieldControlTypeDateTime:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((TextInputGroupTableViewCell *)cell).text] copy];

                        [self packObj:packObj extractDateTimeValue:text fieldDescript:fieldDescript transformMap:transformMap];
                    }
                    break;
                }
                case DynamicFieldControlTypeSingleSelect:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((DropDownGroupTableViewCell *)cell).text] copy];
                        if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                            [packObj setValue:text forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                        else
                            [packObj expandfields][fieldDescript.fieldName] = text;
                    }
                    break;
                }
                case DynamicFieldControlTypeMultiSelect:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSArray *ar = ((MultiSelectGroupTableViewCell *)cell).selectitems;
                        NSString *text = [NSString stringValue:[NSString megerStringWithTag:ar tag:SplitCommaString]];
                        if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                            [packObj setValue:text forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                        else
                            [packObj expandfields][fieldDescript.fieldName] = text;
                    }
                    break;
                }
                case DynamicFieldControlTypePosition:
                {
                    NSString *selectAddress = [[NSString stringValue:((PositionGroupTableViewCell*)cell).selectAddress] copy];
                    NSString *text = [[NSString stringValue:((PositionGroupTableViewCell*)cell).text] copy];
                    
                    [self packObj:packObj extractPostionValue:text selectValue:selectAddress fieldDescript:fieldDescript transformMap:transformMap];
                    
                    break;
                }
                case DynamicFieldControlTypeHeadImage:
                {
                    [packObj setValue:[_headImagePath copy] forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    break;
                }
                case DynamicFieldControlTypeImagePanel:
                {
                    NSArray *ar = ((ImagePanelGroupTableViewCell*)cell).selectedImagePath;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:ar forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = ar;
                    
                    break;
                }
                case DynamicFieldControlTypeMixedPanel:
                {
                    NSArray *ar = ((MixedPanelGroupTableViewCell*)cell).selectedImagePath;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:ar forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = ar;
                    
                    break;
                }
                case DynamicFieldControlTypeSwitchSelect:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                    }
                    break;
                }
                case DynamicFieldControlTypeCreateUUID:
                {
                    NSObject *obj = ((CreateUUIDGroupTableViewCell *)cell).createdObj;
                    NSString *n = [NSString stringWithFormat:@"%@",obj];
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:n forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = n;
                    break;
                }
                case DynamicFieldControlTypeCreateUserID:
                {
                    id obj = ((CreateUserIdGroupTableViewCell *)cell).userId;
                    
                    NSNumber *userStr = [NSNumber new];
                    
                    if ([obj isKindOfClass:[XMSNumberInt class]])
                        userStr = [obj toNSNumber];
                    else
                        userStr = obj;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:userStr forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = userStr;
                    break;
                }
                case DynamicFieldControlTypeCreateUserName:
                {
                    id obj = ((CreateUserNameGroupTableViewCell *)cell).userName;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:obj forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = obj;
                    break;
                }
                case DynamicFieldControlTypeCreateDeptID:
                {
                    id obj = ((CreateDeptIdGroupTableViewCell *)cell).deptId;
                    
                    NSNumber *userStr = [NSNumber new];
                    
                    if ([obj isKindOfClass:[XMSNumberInt class]])
                        userStr = [obj toNSNumber];
                    else
                        userStr = obj;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:userStr forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = userStr;
                    break;
                }
                case DynamicFieldControlTypeCreateDeptName:
                {
                    id obj = ((CreateDeptNameGroupTableViewCell *)cell).deptName;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:obj forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = obj;
                    break;
                }
                case DynamicFieldControlTypeCreateDate:
                {
                    id obj = ((CreateDateGroupTableViewCell*)cell).currentTime;
                    
                    NSString *dateStr = @"";
                    
                    if ([obj isKindOfClass:[NSDate class]])
                        dateStr = [obj toStringDate:yMdHms];
                    else
                        dateStr = obj;
                    
                    if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                        [packObj setValue:dateStr forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                    else
                        [packObj expandfields][fieldDescript.fieldName] = dateStr;
                    
                    break;
                }
                case DynamicFieldControlTypeSingleSelectDic:
                {
                    if(specialExtract(fieldDescript) == NO)
                    {
                        NSString *text = [[NSString stringValue:((DicDropDownGroupTableViewCell *)cell).text] copy];
                        if(fieldDescript.fieldType.intValue != DynamicFieldTypeCustom)
                            [packObj setValue:text forKey:[qdSet getValueForKey:fieldDescript.fieldName]];
                        else
                            [packObj expandfields][fieldDescript.fieldName] = text;
                    }
                    break;
                }
            }
        }
    }
}

@end
